cmake_minimum_required(VERSION 3.12.1)
project(CrossHello)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 设置默认构建类型（可选）
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose build type" FORCE)
endif()

# 设置输出目录
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

# 增加make clean的自定义目标(删除build目录)
add_custom_target(clean-build
    COMMAND ${CMAKE_COMMAND} -E rm -rf ${CMAKE_BINARY_DIR}
    COMMENT "Remove build directories"
)

# 增加make clean的自定义目标（不删除cmake缓存）
add_custom_target(clean-all
    COMMAND ${CMAKE_COMMAND} -E rm -rf ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
    COMMAND ${CMAKE_COMMAND} -E rm -rf ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
    COMMAND ${CMAKE_COMMAND} -E rm -rf ${CMAKE_BINARY_DIR}/install
    COMMENT "Cleaning build and install directories"
)

# 修改当前工程的own为vscode
add_custom_target(chown-vscode
    COMMAND sudo chown -R vscode:vscode .
    COMMENT "sudo chown -R vscode:vscode ."
)

# 添加选项：是否构建静态版本
option(BUILD_STATIC "Build static version of hello and main_static" ON)

# 添加共享库目标
add_library(hello SHARED src/hello.cpp src/calculator.cpp)
# 包含头文件目录
target_include_directories(hello PUBLIC ${CMAKE_SOURCE_DIR}/src/include)
# 编译选项（调试友好）
# target_compile_options(hello PRIVATE -Wall -Wextra -O0 -g)

# 添加动态链接的可执行文件
add_executable(main src/main.cpp src/Arithmetic.cpp)
# main也需要头文件路径
target_include_directories(main PRIVATE ${CMAKE_SOURCE_DIR}/src/include ${CMAKE_SYSROOT}/3rd/usr/include)

# 链接 hello 动态库
target_link_libraries(main PRIVATE hello)

# 编译选项（安全+优化）
target_compile_options(main PRIVATE
    -Wall
    -Werror
    -Wformat
    -Wfloat-equal
    -Wtrampolines
    -fno-strict-aliasing
    -fstack-protector-strong
    -D_FORTIFY_SOURCE=2
    -fPIE
    -fPIC
    # 构建类型相关选项
    $<$<CONFIG:Debug>:-g -O0>
    $<$<CONFIG:Release>:-O2>
    $<$<CONFIG:RelWithDebInfo>:-g -O2>
    $<$<CONFIG:MinSizeRel>:-Os>
)

# 链接选项（安全+PIE）
target_link_options(main PRIVATE
    -pie
    -rdynamic
    -Wl,-z,noexecstack
    -Wl,-z,relro
    -Wl,-z,now
    # 构建类型相关链接选项（如调试符号）
    $<$<CONFIG:Debug>:-g>
)

# 安装动态版本
install(TARGETS hello main
        RUNTIME DESTINATION bin
        LIBRARY DESTINATION lib)

# 将头文件安装到系统路径 
install(DIRECTORY src/include/ DESTINATION include)
     
# 如果启用静态构建
if(BUILD_STATIC)
    # 添加静态库目标
    add_library(hello_static STATIC src/hello.cpp src/calculator.cpp)
    # 包含头文件目录
    target_include_directories(hello_static PUBLIC ${CMAKE_SOURCE_DIR}/src/include)

    # 添加静态链接的可执行文件
    add_executable(main_static src/main.cpp src/Arithmetic.cpp)
    target_include_directories(main_static PRIVATE ${CMAKE_SOURCE_DIR}/src/include ${CMAKE_SYSROOT}/3rd/usr/include)
    target_link_libraries(main_static hello_static)    

    # 安装静态版本
    install(TARGETS hello_static main_static
            RUNTIME DESTINATION bin
            ARCHIVE DESTINATION lib)
endif()

# install三方依赖库
install(FILES 
        ${CMAKE_SYSROOT}/usr/lib64/libstdc++.so
        ${CMAKE_SYSROOT}/usr/lib64/libstdc++.so.6
        ${CMAKE_SYSROOT}/usr/lib64/libstdc++.so.6.0.33
        DESTINATION lib)

# 添加子目录
add_subdirectory(tlv-sdk) 

# test
# add_subdirectory(test)